﻿namespace RPCenter.WindowsAPI
{
    using Microsoft.VisualBasic.CompilerServices; 
    using System;
    using System.Text;

    public class NativeDll
    {
        public static bool DoesMethodExistInDll(string moduleName, string methodName)
        {
            IntPtr moduleHandle = ApiDefinitions.GetModuleHandle(moduleName);
            if (moduleHandle == IntPtr.Zero)
            {
                return false;
            }
            return (ApiDefinitions.GetProcAddress(moduleHandle, methodName) != IntPtr.Zero);
        }

        public static string GetResourceString(string ResourcePath)
        {
            if (!ResourcePath.Contains(","))
            {
                throw new ArgumentException("Resource path is not valid - the path should look like this: \"@%Systemroot%\\system32\\ExampleLibraryName.dll,-10\"", "ResourcePath");
            }
            string[] strArray = ResourcePath.Split(new char[] { ',' });
            IntPtr hInstance = ApiDefinitions.LoadLibrary(Environment.ExpandEnvironmentVariables(strArray[0].Replace("@", "")));
            StringBuilder lpBuffer = new StringBuilder(0x400);
            ApiDefinitions.LoadString(hInstance, Conversions.ToUInteger(strArray[1].Replace("-", "")), lpBuffer, lpBuffer.Capacity + 1);
            ApiDefinitions.FreeLibrary(hInstance);
            return lpBuffer.ToString();
        }
    }
}

